package com.nondo.dean.product.controller.api.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.ValidateUtil;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.product.domain.ProductMix;
import com.nondo.dean.product.model.result.ProductMixResult;
import com.nondo.dean.product.service.ProductMixService;


/**
 * 产品组合管理
 * @author nondo
 * @date 2018年6月25日
 */
@RestController
@RequestMapping("/{v}/admin/product/mix")
public class ProductMixController {

    @Autowired
    private ProductMixService productMixService;

    /**
     * 查看产品组合详情
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/info/{id}")
    public View<ProductMixResult> info(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View<ProductMixResult> view = new View<ProductMixResult>(20000, "获取数据成功");
        
        ProductMixResult productMixResult = null;
        ProductMix productMix = null;
        if (id != null) {
            productMix = productMixService.selectProductMix(id);
            productMixResult = productMixService.getProductMixResult(productMix);
        } else {
            productMixResult = new ProductMixResult();
        }

        view.setData(productMixResult);
        return view;
    }
    
    /**
     * 添加产品组合
     * @param request
     * @param response
     * @param productMix 产品组合信息 {@link ProductMix}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/add")
    public View<ProductMix> add(HttpServletRequest request, HttpServletResponse response,
            @Validated @RequestBody ProductMix productMix, BindingResult result) {
        View<ProductMix> view = new View<ProductMix>(20000, "添加成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            ProductMix productMixDb = this.productMixService.selectProductMixByName(productMix.getMixName());
            if (productMixDb != null) {
                return View.of500Error("产品组合名称已存在");
            }
            this.productMixService.addProductMix(productMix);
        }
        return view;
    }

    /**
     * 更新产品组合
     * @param request
     * @param response
     * @param productMix 产品组合信息 {@link ProductMix}
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/update")
    public View<ProductMix> update(HttpServletRequest request,
            HttpServletResponse response,
            @Validated @RequestBody ProductMix productMix, BindingResult result) {
        View<ProductMix> view = new View<ProductMix>(20000, "更新成功");

        if (result.hasErrors()) {
            view.setCode(40000);
            view.setMessage(ValidateUtil.first(result.getAllErrors()));
        } else {
            ProductMix productMixDb = productMixService.selectProductMix(productMix.getId());
            if (productMixDb != null && productMixDb.getId() != productMix.getId()) {
                return View.of500Error("产品组合名称已存在");
            }
            BeanUtils.copy(productMix, productMixDb);
            this.productMixService.updateProductMix(productMixDb);
        }

        return view;
    }

    /**
     * 获取产品组合列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<ProductMix>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<ProductMix> query) {
        View<Page<ProductMix>> view = new View<Page<ProductMix>>(20000, "获取列表成功");

        ProductMix productMix = query.getData();

        Page<ProductMix> page = new Page<ProductMix>(query.getPage(), query.getLimit(), productMixService.count(productMix));
        
        List<ProductMix> productMixList = productMixService.getProductMixList(query.getOffset(), query.getLimit(), productMix);
        
        page.setContent(productMixList);
        
        view.setData(page);
        
        return view;
    }

    /**
     * 删除产品组合
     * @param request
     * @param response
     * @param id
     * @return
     */
    @ApiVersion(1)
    @SuppressWarnings("rawtypes")
    @RequestMapping("/delete/{id}")
    public View delete(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") Integer id) {
        View view = new View(20000, "删除成功");

        ProductMix productMix = productMixService.selectProductMix(id);
        productMix.setDelFlag(String.valueOf(1));
        this.productMixService.updateProductMix(productMix);

        return view;
    }
    
}
